package com.auth.leaves.controller;

import cn.hutool.core.util.StrUtil;
import com.auth.leaves.pojo.vo.LoginUser;
import com.auth.leaves.response.ResponseDataUtil;
import com.auth.leaves.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

/**
 * Swagger登录接口
 * @Author LEAVES
 * @Date 2020/8/31 20:43
 * @Version 1.0
 */

@Slf4j
@RestController
@CrossOrigin
@Api(value = "系统登录")
public class LoginController {

    @Resource
    private IUserService userService;

    /**
     * 登录
     * @param username
     * @param password
     * @param response
     * @return
     */
    @PostMapping("/swaggerLogin")
    @ApiOperation(value = "获取用户信息", notes = "通过用户ID获取用户信息")
    @ApiImplicitParam(name = "loginUser", value = "用户登录信息", required = true, dataType = "loginUser")
    public Object swaggerLogin(@RequestBody LoginUser loginUser, HttpServletResponse response){
        //用户名和密码非空判断
        if (StrUtil.isNotBlank(loginUser.getUsername()) && StrUtil.isNotBlank(loginUser.getPassword())){
            return userService.login(loginUser.getUsername(), loginUser.getPassword(), response);
        } else {
            log.info("******");
            return ResponseDataUtil.fail("登录失败，用户名或密码为空！");

        }

    }

    /**
     * 登录
     * @param username
     * @param password
     * @param response
     * @return
     */
    @PostMapping("/login")
    public Object login(@RequestParam("username")String username, @RequestParam("password")String password, HttpServletResponse response){
        //用户名和密码非空判断
        if (StrUtil.isNotBlank(username) && StrUtil.isNotBlank(password)){
            return userService.login(username, password, response);
        } else {
            log.info("******");
            return ResponseDataUtil.fail("登录失败，用户名或密码为空！");

        }

    }

}
